<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI Web Develop
 *
 * @package     HITI Web Dev
 * @category    Models
 * @see 		News
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class News_model extends CI_Model {
	public function __contruct()
	{
		parent::__contruct();
	}

	public function show($id, $limit, $get_row = false, $order_mode = 'id') {
        $lng = $GLOBALS['cfg']['lng'];
        $cats = array();
        $this->db->select('id');
        if (is_numeric($id)) {
            $this->db->where('id', $id);
        } else {
            $this->db->where('keyword', $id);
        }
        $query = $this->db->get('article_categories');
        if ($query->num_rows() > 0) {
            $cat = $query->row()->id;
            $cats[] = $cat;
            $this->db->select('id');
            $this->db->where('parent', $cat);
            $query = $this->db->get('article_categories');
            if ($query->num_rows() > 0) {
                foreach ($query->result_array() as $data) {
                    $cats[] = $data['id'];
                }
            }
            $this->db->query('SET NAMES "LATIN1"');
            $this->db->where_in('cat', $cats);
            $this->db->where('deleted', 0);
            $this->db->where('active', 1);
            if (!$get_row) {
                $this->db->select('id, keyword, name_' . $lng . ' AS name, img, des_' . $lng . ' AS des, date_added, (SELECT c.keyword FROM article_categories c WHERE c.id = cat) AS catkey');
                if($cats==$GLOBALS['cfg']['catid_gioithieu']){
                    $this->db->order_by($order_mode . ' desc');
                }else{
                    $this->db->order_by('sort_order desc');
                }
                
                
                $this->db->limit($limit, $GLOBALS['var']['rowstart'] * $limit - $limit);
                $query = $this->db->get('articles');
                if ($query->num_rows() > 0) {
                    return $query->result_array();
                } else {
                    return false;
                }
            } else {
                $this->db->select('id');
                return $this->db->get('articles')->num_rows();
            }
        }
    }

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		$this->db->select('id, keyword, name_'.$lng.' AS name, img, des_'.$lng.' AS des, info_'.$lng.' AS info, cat');
		$this->db->where('deleted', 0);
		if (is_numeric($id))
		{
			$this->db->where('id', $id);
		}
		else
		{
			$this->db->where('keyword', $id);
		}
		$query = $this->db->get('articles');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			return false;
		}
	}

	public function upview($id)
	{
		$this->db->query('UPDATE articles SET view = view + 1 WHERE id = "'.$id.'"', true);
	}

	public function category($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		if (is_numeric($id))
		{
			$this->db->where('id', $id);
		}
		else
		{
			$this->db->where('keyword', $id);
		}
		$this->db->select('id, name_'.$lng.' AS name');
		$query = $this->db->get('article_categories');
		if ($query->num_rows())
		{
			return $query->row_array();
		}
		else
		{
			return false;
		}
	}

	public function list_conmnents($article)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->from('comments AS c1');
		$this->db->select('c1.name, c1.email, c1.info, c1.date_added, c2.user AS user, c2.info as reply, c2.date_added AS reply_time');
		$this->db->join('comments AS c2', 'c1.id = c2.reply', 'left');
		$this->db->where('c1.reply', 0);
		$this->db->where('c1.active', 1);
		$this->db->where('c1.deleted', 0);
		$this->db->where('c1.article', $article);
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function categories($parent = 0)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		$this->db->select('id, keyword, name_'.$lng.' AS name');
		$this->db->where('parent', $parent);
		$this->db->where('deleted', 0);
		$this->db->where('active', 1);
		$this->db->order_by('sort_order asc');
		$query = $this->db->get('article_categories');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}
}

/* End of file news_model.php */
/* Location: ./application/models/news_model.php */